<template>
	<view class="Signin">
		<view class="round round1"></view>
		<view class="round round2"></view>
		<view class="round round3"></view>
		<view class="Signin_bos">
			<image class="logo" :src="dataobj.logo" mode=""></image>
			<view style="margin-top: 20rpx;" class="font15 font-323232 font-b">{{dataobj.name}}</view>
			<button v-if="agree" class="Signin_bnt flex-row-center font15" open-type="chooseAvatar"
				@chooseavatar="onChooseAvatar">
				手机号快捷登录
			</button>
			<view v-else class="Signin_bnt flex-row-center font15" @click="onagree1">手机号快捷登录</view>
			<view class="font14 flex-row-start Signin_info">
				<view class="flex-row-start" @click="onagree">
					<view class="info_border flex-row-center">
						<view v-if="agree"></view>
					</view>
					<view>我已阅读并同意</view>
				</view>
				<view class="font-6ccbb6" @click="onPath">《维普眼健康平台用户服务协议》</view>
			</view>
		</view>
		<!-- 登录弹窗 -->
		<uni-popup ref="popup">
			<view class="popup_bos flex-column-between">
				<view class="font14 font-1e1e1e">登录授权 申请</view>
				<view class="popup_box flex-row-start">
					<image :src="getUserObj.headimgurl" mode=""></image>
					<input @input="onnickname" @change="onnickname" v-model="getUserObj.nickname" type="nickname"
						class="font14" placeholder="请输入昵称" />
					<!-- <view class="font14 font-1e1e1e">{{getUserObj.nickname}}</view> -->
				</view>
				<view class="popup_button flex-row-around">
					<view class="flex-row-center font12" @click="Onclose">拒绝</view>
					<button v-if="getUserObj.nickname" class="flex-row-center font12 font-ffffff"
						open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">同意</button>
					<button @click="onName" v-else class="flex-row-center font12 font-ffffff">同意</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dataobj: {},
				signinif: true,
				wxcode: '', //微信code
				//授权返回
				getUserObj: {
					nickname: '',
					headimgurl: '',
					sex: ''
				},

				uid: '',
				agree: false,
			}
		},
		onShow: function() {
			var that = this
			wx.login({
				success: res => {
					that.wxcode = res.code
				}
			})
		},
		onLoad() {
			//接口
			this.Onhttp()
		},
		methods: {
			onagree() {
				this.agree = !this.agree
			},
			onagree1() {
				uni.showToast({
					title: '请勾选维普眼健康平台用户服务协议',
					icon: 'none'
				})
			},
			onPath(){
				uni.navigateTo({
					url: '/pages/home/platformProtocol?type=true'
				})
			},
			onChooseAvatar(e) {
				let that = this
				if (e.detail) {
					uni.uploadFile({
						url: that.$Config.http_static+'api/common/uploader',
						filePath: e.detail.avatarUrl,
						name: 'file',
						success: function(res1) {
							let resdata = JSON.parse(res1.data)
							if (resdata.code == 1) {
								that.getUserObj.headimgurl = resdata.data.fullurl
								that.$refs.popup.open()
							} else {
								uni.showToast({
									title: resdata.msg,
									icon: 'none',
									duration: 2000
								})
							}
						},
						fail: function(res) {}
					})
				
				}
			},
			onnickname(e) {
				this.getUserObj.nickname = e.detail.value
			},
			onName() {
				uni.showToast({
					title: '请输入昵称~',
					icon: 'none'
				})
				return
			},
			//接口
			Onhttp() {
				this.$Http({
					url: 'hospital',
					data: {}
				}).then(res => {
					if (res.code == 1) {
						res.data.logo = this.$store.state.http_img + res.data.logo
						this.dataobj = res.data
					}
				});
			},
			//一键登录
			getPhoneNumber(e) {
				var that = this
				var data = {
					nickname: this.getUserObj.nickname,
					sex: this.getUserObj.sex,
					headimgurl: this.getUserObj.headimgurl,
				}
				console.log(data, '我是请求参数~')
				//用户通过
				if (e.detail.errMsg == 'getPhoneNumber:ok') {
					data.iv = e.detail.iv
					data.encryptedData = e.detail.encryptedData
					data.code = that.wxcode
					data.spuid = that.$store.state.spuid
					this.$Http({
						url: 'wechatLogin',
						// type: 'POST',
						data: data
					}).then(res => {
						if (res.code == 1) {
							uni.setStorage({
								key: 'token',
								data: res.data.token,
								success: function() {
									that.$store.dispatch('userBind')
									that.OnUserInfo()
									that.signinif = true
								}
							});
						}
					});
				}
			},
			Onphonecode(e) {
				var that = this
				this.$Http({
					url: 'testLogin',
					// type: 'POST',
					data: {
						uid: this.uid
					}
				}).then(res => {
					if (res.code == 1) {
						uni.setStorage({
							key: 'token',
							data: res.data.token,
							success: function() {
								that.OnUserInfo()
							}
						});
					}
				});
			},
			Onclose() {
				this.signinif = true
				this.$refs.popup.close()
			},
			//用户信息
			OnUserInfo() {
				this.$store.dispatch('userBind')

				let that = this
				this.$Http({
					url: 'getUserInfo',
					type: 'POST',
					data: {}
				}).then(res => {
					if (res.code == 1) {
						uni.setStorage({
							key: 'userInfo',
							data: res.data,
							success: function() {
								if (that.$store.state.url != '') {
									if (that.$store.state.url == 'pages/home/index' || that.$store
										.state.url ==
										'pages/message/chatList' || that.$store.state.url ==
										'pages/my/index' ||
										that.$store.state.url == 'pages/doctor/index' || that.$store
										.state.url == 'pages/doctor/my') {
										uni.switchTab({
											url: '/' + that.$store.state.url,
										})
									} else {
										uni.redirectTo({
											url: '/' + that.$store.state.url,
										})
									}
									that.$store.commit('Onvuexurl', '')
								} else {
									//如果是医生直接去医生端
									if (res.data.is_doctor == 1) {
										uni.switchTab({
											url: '/pages/doctor/index'
										})
										return
									}
									//如果是药师
									if (res.data.is_pharmacist == 1) {
										uni.switchTab({
											url: '/pagesA/prescript/index'
										})
										return
									}
									uni.switchTab({
										url: '/pages/my/index'
									})
								}
							}
						});
					}
				});
			}
		}
	}
</script>

<style lang="less">
	@import url("style.less");
</style>